/**
 *
 */
package com.angel.email.builder.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import com.angel.common.keyValue.KeyValueResult;
import com.angel.email.account.AccountInformation;
import com.angel.email.authenticator.EmailAuthenticator;
import com.angel.email.builder.EmailBuilder;
import com.angel.email.configuration.builders.OutgoingEmailConfigurationBuilder;
import com.angel.email.exceptions.EmailAccessException;



/**
 * @author William
 *
 */
public class SimpleEmailBuilder implements EmailBuilder{

	private OutgoingEmailConfigurationBuilder configurationBuilder;
	private List<String> toEmails;
	private String message;
	private String subject;
	private Date sentDate;

	public SimpleEmailBuilder(OutgoingEmailConfigurationBuilder configurationBuilder, String message, List<String> toEmails, String subject){
		super();
		this.message = message;
		this.toEmails = toEmails;
		this.subject = subject;
		this.sentDate = new Date();
		this.configurationBuilder = configurationBuilder;
	}


	public Email buildEmail() {
		SimpleEmail email = new SimpleEmail();
		AccountInformation accountInformation = this.configurationBuilder.getAccountInformation();
		KeyValueResult parameters = this.configurationBuilder.buildOutgoingEmailConfiguration();
		EmailAuthenticator a = (EmailAuthenticator) this.configurationBuilder.buildAuthenticator();
		email.setAuthenticator(a);
		email.setSmtpPort(Integer.valueOf(parameters.getResultFor("mail.outgoing.port").toString()));
		email.setHostName(parameters.getResultFor("mail.outgoing.url").toString());
		email.setSubject(this.getSubject());
		email.setDebug(true);
		try {
			email.setFrom(accountInformation.getEmailAccount(), accountInformation.getName());
			for(String toEmail: this.getToEmails()){
				email.addTo(toEmail);
			}
			email.setMsg(this.getMessage());
		} catch (EmailException e) {
			//TODO Change exception name.
			throw new EmailAccessException("", e);
		}
		return email;
	}

	/**
	 * @return the configurationBuilder
	 */
	protected OutgoingEmailConfigurationBuilder getConfigurationBuilder() {
		return configurationBuilder;
	}

	/**
	 * @param configurationBuilder the configurationBuilder to set
	 */
	protected void setConfigurationBuilder(
			OutgoingEmailConfigurationBuilder configurationBuilder) {
		this.configurationBuilder = configurationBuilder;
	}

	/**
	 * @return the toEmails
	 */
	public List<String> getToEmails() {
		return toEmails;
	}

	/**
	 * @param toEmails the toEmails to set
	 */
	public void setToEmails(List<String> toEmails) {
		this.toEmails = toEmails;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * @param subject the subject to set
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @return the sentDate
	 */
	public Date getSentDate() {
		return sentDate;
	}

	/**
	 * @param sentDate the sentDate to set
	 */
	public void setSentDate(Date sentDate) {
		this.sentDate = sentDate;
	}
}
